from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relation
from divirt import sqlalchemy_meta_data
from divirt import logging
from host import Host

log = logging.getLogger('divirt.components.switch')

Base = declarative_base(metadata=sqlalchemy_meta_data)
class Switch(Base):
    # Table
    __tablename__ = 'switches'
    id = Column(Integer, primary_key=True)
    host_id = Column(Integer, ForeignKey('hosts.id'))
    comm_sock = Column(String)
    mgmt_sock = Column(String)
    pid_file = Column(String)
    
    # Relations
    host = relation(Host, backref=backref('switches', order_by=id))

    def __init__(self, host_id=None, comm_sock=None, mgmt_sock=None, 
                 pid_file=None):
        self.host_id = host_id
        self.comm_sock = comm_sock
        self.mgmt_sock = mgmt_sock
        self.pid_file = pid_file

    def __repr__(self):
        return '<Switch(id=%s, host_id=%s, comm_sock=%s, mgmt_sock=%s, ' \
            'pid_file=%s)>' % (self.id, self.host_id, self.comm_sock,
                               self.mgmt_sock, self.pid_file)
